package com.tanjie.blackbean.iot.demo.controller;

import com.tanjie.blackbean.iot.demo.support.MqttUtil;
import com.tanjie.blackbean.web.annotation.BlackBeanController;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;

/**
 * @Author tanjie
 * @Date 2022/6/18 下午3:08 周六
 **/
@Slf4j
@BlackBeanController(skipAuth = true)
public class HelloController {

    @Resource
    MqttClient mqttClient;

    @GetMapping("hello")
    public String sayHelloHandler(@RequestParam String name) {
        new Thread(() -> {
            log.info("thread1 hello");
            MqttUtil.publish(mqttClient,"thread1", "blackbean/hello");
        }, "thread1").start();
        new Thread(() -> {
            log.info("thread2 hello");
            MqttUtil.publish(mqttClient,"thread2", "blackbean/hello");
        }, "thread2").start();
        new Thread(() -> {
            log.info("thread3 hello");
            MqttUtil.publish(mqttClient,"thread3", "blackbean/hello");
        }, "thread3").start();
        new Thread(() -> {
            log.info("thread4 hello");
            MqttUtil.publish(mqttClient,"thread4", "blackbean/hello");
        }, "thread4").start();
        return "hello " + name;
    }

}
